# -*- coding: utf-8 -*-

###########################################################
#                                                         #
# shooter - shooter game                                  #
#                                                         #
# 2011 - Andreas Grätz                                    #
#                                                         #
# andreaszdw@googlemail.com                               #
#                                                         #
# playstate.py                                            #
#                                                         #
###########################################################

from PySFML import sf
import state
import playstate
import planetsystem
import random
import math

class PlayState(state.State):
	
	def __init__(self):
		print "playstate - __init__"
	
	def __del__(self):
		pass
		
	def init(self, window):
		self.count_init = 0
		self.running = True
		self.testTurn = 0
		self.turnBody = 0
		self.ps = planetsystem.PlanetSystem()
		self.nextState = False
		self.popState = False
		
		self.Font = sf.Font()
		self.Font.LoadFromFile("../font/Ubuntu-L.ttf")
		self.Text = sf.String()
		self.Text.SetText("FPS:")
		self.Text.SetFont(self.Font)
		self.Text.SetPosition(10, 10)
		
		self.image_body = sf.Image()
		self.image_body.LoadFromFile("../gfx/ship_body_v2_64x64.png")
		self.sprite_body =sf.Sprite(self.image_body)
		self.sprite_body.SetCenter(26, 32)
		self.sprite_body.SetPosition(window.GetWidth()/2, window.GetHeight()/2)
		
		self.image = sf.Image()
		self.image.LoadFromFile("../gfx/ship_turret_32x32.png")
		self.sprite = sf.Sprite(self.image)
		self.sprite.SetCenter(16, 8)
		self.sprite.SetPosition(window.GetWidth()/2, window.GetHeight()/2)
		
		self.clock = sf.Clock()
		self.clock2 = sf.Clock()

		self.turn = 0
		self.frame = 0
		self.frame2 = 0
		self.clock.Reset()
		self.clock2.Reset()
	
	def cleanup(self):
		print "playstate: I clean up"
		
	def handleevents(self, window):	
		event = sf.Event()
		
		while window.GetEvent(event):
			if event.Type == sf.Event.Closed:
				self.running = False

			if event.Type == sf.Event.KeyPressed:
				if event.Key.Code == sf.Key.Escape:
					self.running = False
				if event.Key.Code == sf.Key.Left or event.Key.Code == sf.Key.A:
					self.turnBody = 80
				if event.Key.Code == sf.Key.Right or event.Key.Code == sf.Key.D:
					self.turnBody = -80
				if event.Key.Code == sf.Key.R:
					self.popState = True
					
		
			if event.Type == sf.Event.KeyReleased:
				if event.Key.Code == sf.Key.Left:
					self.turnBody = 0
				if event.Key.Code == sf.Key.Right:
					self.turnBody = 0
				if event.Key.Code == sf.Key.A:
					self.turnBody = 0
				if event.Key.Code == sf.Key.D:
					self.turnBody = 0
			
			if event.Type == sf.Event.MouseMoved:
				tmpMouseX = event.MouseMove.X
				tmpMouseY = event.MouseMove.Y
				tmpCenterX = window.GetWidth()/2
				tmpCenterY = window.GetHeight()/2
				self.testTurn = math.degrees(math.atan2(tmpMouseX - tmpCenterX, tmpMouseY -tmpCenterY))
	
	def update(self, window):
		self.sprite.SetRotation(self.testTurn)
		self.sprite_body.Rotate(self.turnBody * window.GetFrameTime())
	
	def pause(self):
		print "playstate.py - pause"
	
	def resume(self):
		print "playstate.py - resume"
	
	def draw(self, window):
		window.Clear()
		self.ps.draw(window)
		window.Draw(self.sprite_body)
		window.Draw(self.sprite)
		window.Draw(self.Text)
		window.Display()
		self.frame += 1
		self.frame2 += 1
		if self.clock.GetElapsedTime() > 1:
			self.Text.SetText("Fps: %i" % self.frame)
			print "vector %f/%f" % (math.cos(math.radians(self.sprite_body.GetRotation())), math.sin(math.radians(self.sprite_body.GetRotation())))
			self.frame = 0
			self.clock.Reset()

	def isrunning(self):
		return self.running
